﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Power.Common;
using JN.Utility;
using DevExpress.XtraTreeList.Nodes;
using System.Net.NetworkInformation;

namespace Power.ComManager
{
    public partial class NodeList : DevExpress.XtraEditors.XtraForm
    {
        #region property
        BLL.Node_infoBLL nodeBLL = new BLL.Node_infoBLL();
        #endregion
        public NodeList()
        {
            InitializeComponent();
        }

        private void NodeList_Load(object sender, EventArgs e)
        {
            //load line
            using (new DevExpress.Utils.WaitDialogForm("正在加载数据...", "Please Wait..."))
            {
                LoadLine();
            }
        }
        /// <summary>
        /// 加载line节点
        /// </summary>
        private void LoadLine()
        {
            try
            {
                TreeListNode node;
                treeList1.Nodes.Clear();
                node = treeList1.AppendNode(new object[] { "线路列表", "线路列表", null, null }, null);
                node.StateImageIndex = 1;

                //string[] ports = System.IO.Ports.SerialPort.GetPortNames();
                DataTable dtData = nodeBLL.GetList(" station_id='" + Manager.SystemStatus.Instance.Station_id + "' ").Tables[0];
                for (int i = 0; i < dtData.Rows.Count; i++)
                {
                    node = treeList1.AppendNode(new object[] { dtData.Rows[i]["node_ip"], dtData.Rows[i]["node_ip"], null, null }, 0);
                    node.StateImageIndex = 0;
                }
                treeList1.ExpandAll();
                this.treeList1.EndUnboundLoad();
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }
        /// <summary>
        /// treelist click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeList1_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnPing.Text =="测试中")
                {
                    XtraMessageBox.Show("测试中，不能切换!");
                    return;
                }
                TreeListNode clickedNode = this.treeList1.FocusedNode;
                string disPlayText = clickedNode.GetDisplayText(0);
                if (disPlayText.ToUpper() != "线路列表")
                {
                    //显示text
                    //DisplayForm(disPlayText.Substring(0, clickedNode.GetDisplayText(0).IndexOf("_")));
                    txtIp.Text = disPlayText;
                }
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }
        /// <summary>
        /// ping
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPing_Click(object sender, EventArgs e)
        {
            if(txtIp.Text == "")
            {
                XtraMessageBox.Show("请输入IP地址!!","提示");
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            btnPing.Text = "测试中";
            PingHost();
            btnPing.Text = "测试";
            this.Cursor = Cursors.Default;
        }
        private void PingHost()
        {
            try
            {
                lstResult.Items.Clear();
                lstResult.ForeColor = Color.Black;

                lstResult.Items.Add("开始测试...");
                Ping ipPing = new Ping();
                PingReply reply = ipPing.Send(txtIp.Text);
                if (reply.Status == IPStatus.Success)
                {
                    lstResult.Items.Add("IP:"+txtIp.Text + " 线路正常!");
                }
                else
                {
                    lstResult.ForeColor = Color.Red;
                    lstResult.Items.Add("IP"+txtIp.Text + "线路不通，请检查防火墙和网络!!");
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
                SystemLogger.Write(ex);
            }
        }
    }
}